package com.ruoyi.back.service.impl;

import java.io.File;
import java.util.List;

import com.ruoyi.back.domain.Curriculum;
import com.ruoyi.back.mapper.CurriculumMapper;
import com.ruoyi.back.service.ICurriculumService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ffmpeg.FFmpegUtil;
import com.ruoyi.common.utils.uuid.SnowflakeIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 课程信息Service业务层处理
 * 
 * @author luguanqing
 * @date 2021-04-06
 */
@Service
public class CurriculumServiceImpl implements ICurriculumService
{
    @Autowired(required = false)
    private CurriculumMapper curriculumMapper;

    /**
     * 查询课程信息
     * 
     * @param id 课程信息ID
     * @return 课程信息
     */
    @Override
    public Curriculum selectCurriculumById(String id)
    {
        return curriculumMapper.selectCurriculumById(id);
    }

    /**
     * 查询课程信息列表
     * 
     * @param curriculum 课程信息
     * @return 课程信息
     */
    @Override
    public List<Curriculum> selectCurriculumList(Curriculum curriculum)
    {
        return curriculumMapper.selectCurriculumList(curriculum);
    }

    /**
     * 新增课程信息
     * 
     * @param curriculum 课程信息
     * @return 结果
     */
    @Override
    public int insertCurriculum(Curriculum curriculum)
    {
        //获取视频url
        String url = curriculum.getUrl();
        String profile = RuoYiConfig.getProfile();
        //全路径
        url = StringUtils.replace(url, "/profile", profile);
        String preffix = StringUtils.substring(url, 0, url.lastIndexOf("/") + 1);
        String suffix = StringUtils.substring(url, url.lastIndexOf("."), url.length());
        String shorturl = String.valueOf(new SnowflakeIdUtils(0, 0).nextId()) + suffix;
        //截取后视频路径
        shorturl = preffix + shorturl;
        //视频长度
        Integer videoLong = FFmpegUtil.getVideoTime(url);
        //视频长度大于20分钟，截取5分钟
        if(videoLong > 1200){
            FFmpegUtil.cutVideo(new File(url), new File(shorturl), 300);
        }else{ //小于20分钟截取视频时长10%
            FFmpegUtil.cutVideo(new File(url), new File(shorturl), videoLong/10);
        }
        curriculum.setShorturl(StringUtils.replace(shorturl, profile, "/profile"));
        return curriculumMapper.insertCurriculum(curriculum);
    }

    /**
     * 修改课程信息
     * 
     * @param curriculum 课程信息
     * @return 结果
     */
    @Override
    public int updateCurriculum(Curriculum curriculum)
    {
        return curriculumMapper.updateCurriculum(curriculum);
    }


    /**
     *
     * @param id 课程id
     * @return
     */
    @Override
    public int updateVisit(String id) {
        return curriculumMapper.updateVisit(id);
    }

    /**
     *
     * @param id 课程id
     * @return
     */
    @Override
    public int updateOrder(String id) {
        return curriculumMapper.updateOrder(id);
    }

    /**
     * 批量删除课程信息
     * 
     * @param ids 需要删除的课程信息ID
     * @return 结果
     */
    @Override
    public int deleteCurriculumByIds(String[] ids)
    {
        return curriculumMapper.deleteCurriculumByIds(ids);
    }

    /**
     * 删除课程信息信息
     * 
     * @param id 课程信息ID
     * @return 结果
     */
    @Override
    public int deleteCurriculumById(String id)
    {
        return curriculumMapper.deleteCurriculumById(id);
    }
}
